<template>
  <div id="app">
    <!-- 头部 -->
    <Topbar :key="topbarkey"></Topbar>
    <Header></Header>
    <router-view @updatekey="updatekeyFn" />
    <!-- 底部 -->
    <Footer></Footer>
    <!-- 调用登录组件 -->
    <Login v-show="isShowLoginModal"></Login>
    <!-- 增加 离场进场效果 -->
    <transition name="slide">
      <!-- 调用提示框组件 -->
      <Toast v-show="isShowToast"></Toast>
    </transition>
  </div>
</template>
<script>
// 导入组件
import Topbar from "./components/Topbar.vue";
import Footer from "./components/Footer.vue";
import Header from "./components/Header.vue";
import Login from "./components/Login.vue";
import Toast from "./components/Toast.vue";

// 接口地址
import { JingPingAPI, ReMenAPI } from "@/request/api.js";
// 辅助函数
import { mapState } from "vuex";
export default {
  components: {
    // 注册组件
    Topbar,
    Header,
    Footer,
    Login,
    Toast,
  },
  data() {
    return {
      topbarkey: 0,
    };
  },
  methods: {
    updatekeyFn() {
      this.topbarkey++;
    },
  },
  async created() {
    console.log("当前环境为：", process.env.VUE_APP_BASE_URL);
    console.log("名字是", process.env.VUE_APP_MY_NAME);
    // axios
    //   .get("http://kumanxuan1.f3322.net:8881/cms/products/recommend")
    //   .then((res) => {
    //     console.log("请求结果：", res.data.data.data.records);
    //   });
    // 使用封装的 axios
    // JingPingAPI().then((res) => {
    //   console.log("请求结果1是：", res.data);
    // });
    // ReMenAPI().then((res) => {
    //   console.log("请求结果3是：", res.data);
    // });
    // let res = await JingPingAPI();
    // console.log("1", res);
    // console.log("2");
    // let res2 = await ReMenAPI();
    // console.log("3", res2);
    // console.log("4");
  },
  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.ShowModal.isShowLoginModal,
      isShowToast: (state) => state.ShowToast.isShowToast,
    }),
  },
  // 会出现一个bug,刷新时，也会触发一次watch，导致一共触发两次 topbar的create钩子
  // 现象：扫码时 有两次登录请求
  watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        console.log("地址变化了", newVal, oldVal);
        // 如果旧地址为/ 不需要进行重载
        if (oldVal != "/") {
          this.topbarkey++;
        }
      },
    },
  },
};
</script>
<style lang="less">
/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
